<template>
	<view class="">
		
		<view class="search-box" style="background-color: #c00000;">
			<uni-search-bar  @input="input" :radius="100" cancelButton="none" placeholder="请输入内容"></uni-search-bar>
		</view>
		<scroll-view scroll-y="true" v-if="kw.length !==0" >
			<view class="search-list">
				<view @click="gotoDetail(item)" class="search-item" v-for="(item,index) in searchResult" :key="index">
					<view class="search-item-text">
						{{item.goods_name}}
					</view>
					<uni-icons type="arrowright"></uni-icons>
				</view>
			</view>
	
			
		</scroll-view>
		<view class="history-box" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" @click="removeAllHistory"></uni-icons>
			</view>
			<view class="hlist" >
				<uni-tag @click="gotoGoodsList(item)" :text='item' type="" v-for="(item,index) in historys" :key="index"></uni-tag>
				
			</view>
		</view>
	</view>
	
	
</template>

<script>
	export default {
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw')||[]);
		},
		data() {
			return {
				timer:null,
				kw:'',
				searchResult:[],
				historyList:[]
			};
		},
		//定义一个计算属性
		computed: {
			historys(){
				return [...this.historyList].reverse()
			}
		},
		
		methods: {
			gotoGoodsList(item) {
				uni.navigateTo({
					url: '../goods_list/goods_list?query='+item
				})
			},
			removeAllHistory() {
				this.historyList = [];
				uni.setStorageSync('[]')
			},
			saveSearchHistory(){
				const set = new Set(this.historyList);
				set.delete(this.kw);
				set.add(this.kw);
				this.historyList = Array.from(set);
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			
			gotoDetail(item) {
				console.log(item)
				uni.navigateTo({
					url:"../goods_detail/goods_detail?goods_id="+item.goods_id
				})
			},
			input(e) {
				//防抖处理
				clearTimeout(this.timer)
				this.timer = setTimeout(()=> {
					this.kw = e;
					this.getSearchList();
				},500);
			},
			async getSearchList(){
				if (this.kw === '') {
					this.searchResult = [];
					return
				}
				this.historyList.push(this.kw);
				 this.saveSearchHistory();
			const { data: res } = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.kw })
				
				if (res.meta.status != 200)return;
				this.searchResult = res.message
				
			}
		}
	}
</script>

<style lang="scss">
	.history-box {
		padding: 0 5px;
		.history-title {
			display: flex;
			justify-content: space-between;
			margin: 0 7px;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
			height: 30px;
			line-height: 30px;
			
		}
		.hlist {
			width: 100%;
			display: flex;
			flex-wrap: wrap;
			font-size: 12px;
			uni-tag {
				margin: 3px;
			}
			// justify-items: flex-start;
		}
	}
	
	
	
	.search-box{
		position: sticky;
		z-index: 100;
		top: 0;
		height: 50px;
		// width: 100%;
	} 
	.search-item {
		line-height: 45px;
		height: 40px;
		margin: 3px 5px;
		// width: 100%;
		display: flex;
		// flex-wrap: nowrap;
		// margin: 0 10px;
		align-items: center;
		justify-content: space-between;
		font-size: 12px;
		border-bottom: 1px solid #efefef ;
		// justify-content: space-around;
	}
	.search-item-text {
		white-space: nowrap;//不允许换行
		overflow: hidden;
		text-overflow: ellipsis;//超出 ...
		margin-right: 3px;
		// width: 90%;
		// overflow:hidden;
		// text-overflow:ellipsis;
		// white-space:nowrap;
	}
	// uni-search-bar {
	// 	width: 100%;
	// }
</style>
